﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Web;
using System.Runtime.InteropServices;
using System.Drawing;

namespace QD.Web.Upload
{
    public class ImageUpload : FileUpload
    {
        public static NameValueCollection GetFilePath(HttpContext ctx)
        {
            return null;
        }

        protected override void PageLoad()
        {
            string str = base.Context.Request.QueryString[null];
            if (!string.IsNullOrEmpty(str))
            {
                if (str.Equals("Save", StringComparison.OrdinalIgnoreCase))
                {
                    this.SaveThumbsFromRequest();
                }
            }
            else
            {
                UploadCfg uploadConfig = base.GetUploadConfig(UploadType.Image);

                if (base.Context.Request.Files.Count == 0)
                {
                    throw new ArgumentNullException("FileData not found");
                }
                HttpPostedFile httpfile = base.Context.Request.Files[0];
                int index = 0;
                if (uploadConfig.Mode == UploadMode.Each)
                {
                    index = base.Parse.Int("idx", 0);
                }
                ActionResultUpload upload = Upload.UploadImgFile(httpfile, uploadConfig.FolderCfg, uploadConfig.Configs[index] as ImageFileCfg);
                IDictionary<int, string> dictionary = new Dictionary<int, string>(10);
                dictionary[1] = "/Upload/User/";
                dictionary[2] = "/Upload/Game/";
                dictionary[3] = "/Upload/Ads/";
                dictionary[4] = "/Upload/Gift/";
                UploadResult result3 = new UploadResult();
                result3.Module = this.moduleCode.Value;
                result3.Type = 1;
                UploadResult result = result3;
                if (!upload.IsError)
                {
                    if (base.Context.Request["Save"] != null)
                    {
                        List<ThumbResult> list = new List<ThumbResult>();
                        ActionResultUpload upload2 = null;
                        ImageFileCfg[] thumbsCfg = (uploadConfig.Configs[index] as ImageFileCfg).ThumbsCfg;
                        if (thumbsCfg != null)
                        {
                            foreach (ImageFileCfg cfg2 in thumbsCfg)
                            {
                                upload2 = Upload.UploadImgFile(httpfile, uploadConfig.FolderCfg, cfg2);
                                ThumbResult item = new ThumbResult();
                                item.AbsolutePath = ("http://" + base.Context.Request.Url.Authority + dictionary[this.moduleCode.Value] + upload2.SaveAt + upload2.SaveName + upload2.Ext).Replace(@"\", "/");
                                item.FileName = upload2.SaveName + upload2.Ext;
                                item.FilePath = (upload2.SaveAt + upload2.SaveName + upload2.Ext).Replace(@"\", "/");
                                item.Key = cfg2.Key;
                                list.Add(item);
                            }
                            base.Context.Response.Write(this.ToJson(list));
                            base.Context.Response.End();
                        }
                    }
                    result.Code = 0;
                    result.AbsolutePath = ("http://" + base.Context.Request.Url.Authority + dictionary[this.moduleCode.Value] + upload.SaveAt + upload.SaveName + upload.Ext).Replace(@"\", "/");
                    result.FilePath = (upload.SaveAt + upload.SaveName + upload.Ext).Replace(@"\", "/");
                    result.FileName = upload.SaveName + upload.Ext;
                    result.Msg = "ok";
                    result.Key = (uploadConfig.Configs[index] as ImageFileCfg).Key;
                }
                else
                {
                    result.Code = -1;
                    result.Msg = upload.Error;
                }
                base.Context.Response.Write(this.ToJson(result));
                base.Context.Response.End();
            }
        }
        private void SaveThumbs(int module, string filename, int? x1 = null, int? y1 = null, int? x2 = null, int? y2 = null, int? w = null, int? h = null)
        {
            UploadCfg cfg = FileUpload.ImageConfigs[module];
            List<ThumbResult> list = new List<ThumbResult>();
            foreach (ImageFileCfg cfg2 in cfg.Configs)
            {
                if (cfg2.ThumbsCfg != null)
                {
                    System.Drawing.Image image = System.Drawing.Image.FromFile(cfg.FolderCfg.RootPath + filename);
                    double num = ((double)image.Width) / ((double)w.Value);
                    double num2 = ((double)image.Height) / ((double)h.Value);
                    string str = null;
                    foreach (ImageFileCfg cfg3 in cfg2.ThumbsCfg)
                    {
                        int? nullable3;
                        double? nullable4;
                        int? nullable = x2;
                        int? nullable2 = x1;
                        nullable = (nullable.HasValue & nullable2.HasValue) ? new int?(nullable.GetValueOrDefault() - nullable2.GetValueOrDefault()) : ((int?)(nullable3 = null));
                        double num5 = num;
                        nullable = y2;
                        nullable2 = y1;
                        nullable = (nullable.HasValue & nullable2.HasValue) ? new int?(nullable.GetValueOrDefault() - nullable2.GetValueOrDefault()) : ((int?)(nullable3 = null));
                        num5 = num2;
                        Size size = Image.ResizeImage(Convert.ToInt32(nullable.HasValue ? new double?(((double)nullable.GetValueOrDefault()) * num5) : (nullable4 = null)), Convert.ToInt32(nullable.HasValue ? new double?(((double)nullable.GetValueOrDefault()) * num5) : (nullable4 = null)), cfg3.ImageMaxWidth, cfg3.ImageMaxHeigth);
                        image.Dispose();
                        nullable = x1;
                        num5 = num;
                        nullable = y1;
                        num5 = num2;
                        nullable = x2;
                        nullable2 = x1;
                        nullable = (nullable.HasValue & nullable2.HasValue) ? new int?(nullable.GetValueOrDefault() - nullable2.GetValueOrDefault()) : ((int?)(nullable3 = null));
                        num5 = num;
                        nullable = y2;
                        nullable2 = y1;
                        nullable = (nullable.HasValue & nullable2.HasValue) ? new int?(nullable.GetValueOrDefault() - nullable2.GetValueOrDefault()) : ((int?)(nullable3 = null));
                        num5 = num2;
                        str = Upload.SaveCutPic2(cfg.FolderCfg.RootPath + filename, cfg.FolderCfg, Convert.ToInt32(nullable.HasValue ? new double?(((double)nullable.GetValueOrDefault()) * num5) : (nullable4 = null)), Convert.ToInt32(nullable.HasValue ? new double?(((double)nullable.GetValueOrDefault()) * num5) : (nullable4 = null)), Convert.ToInt32(nullable.HasValue ? new double?(((double)nullable.GetValueOrDefault()) * num5) : (nullable4 = null)), Convert.ToInt32(nullable.HasValue ? new double?(((double)nullable.GetValueOrDefault()) * num5) : (nullable4 = null)), size.Width, size.Height);
                        ThumbResult item = new ThumbResult();
                        item.AbsolutePath = cfg.FolderCfg.RootPath + str;
                        item.FileName = str.Replace(@"\", "/");
                        item.FilePath = (cfg.FolderCfg.PathStyle + str).Replace(@"\", "/");
                        item.Key = cfg3.Key;
                        list.Add(item);
                    }
                }
            }
            base.Context.Response.Write(this.ToJson(list.ToArray()));
            base.Context.Response.End();
        }


        public void SaveThumbsFromRequest()
        {
            HttpContext context = base.Context;
            int module = int.Parse(context.Request["jUpload_MODULE"]);
            int num2 = (int)float.Parse(context.Request["jUpload_x1"]);
            int num3 = (int)float.Parse(context.Request["jUpload_y1"]);
            int num4 = (int)float.Parse(context.Request["jUpload_x2"]);
            int num5 = (int)float.Parse(context.Request["jUpload_y2"]);
            int num6 = (int)float.Parse(context.Request["jUpload_w"]);
            int num7 = (int)float.Parse(context.Request["jUpload_h"]);
            string filename = base.Parse.String("jUpload_FILENAME");
            this.SaveThumbs(module, filename, new int?(num2), new int?(num3), new int?(num4), new int?(num5), new int?(num6), new int?(num7));
        }

    }
}
